import re


def parse_curl(curl_command: str, ignore_headers: list = [], ignore_data: bool = True):
    method = None
    url = None
    headers = None
    data = None

    # 正则表达式匹配 curl 命令中的各个部分
    method_pattern = re.compile(r"^curl\s+-X\s+(\w+)", re.IGNORECASE | re.MULTILINE)
    url_pattern = re.compile(r"^curl\s+\'([^\']+)\'", re.MULTILINE)
    header_pattern = re.compile(r"-H\s+\'([^:\']+):\s*([^\\\']*)\'", re.MULTILINE)
    data_pattern = re.compile(r"--data\s+\'([^\']+)\'", re.MULTILINE)

    # 提取 HTTP 方法
    method_match = method_pattern.search(curl_command)
    method = method_match.group(1).upper() if method_match else "GET"

    # 提取 URL
    url_match = url_pattern.search(curl_command)
    url = url_match.group(1) if url_match else None

    if not url:
        raise ValueError("URL not found in the curl command")

    # 提取请求头
    headers = {}
    for match in header_pattern.finditer(curl_command):
        key, value = match.groups()
        name = key.strip()
        if name not in ignore_headers:
            headers[name] = value.strip()

    # 提取请求体
    data = None
    if not ignore_data:
        data_match = data_pattern.search(curl_command)
        data = data_match.group(1) if data_match else None
    return method, url, headers, data
